﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

using Gift.WebApp;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class companyMoneyList : BaseAdmin
    {
       

      public  string ParamentType
        {
            get {
                return Request.QueryString["type"];
            }
        }

      bool _isInMoney = false;
      bool _isOutMoney = false;

      string CompanyParamId
      {
          get
          {
              if (!string.IsNullOrEmpty(Request.QueryString["id"]))
              {
                  return Request.QueryString["id"];
              }
              else
              {
                  if (string.IsNullOrEmpty(base.Administrator.CompanyId))
                  {
                      return "0";
                  }
                  else
                  {
                      return base.Administrator.CompanyId;
                  }
              }
          }
      }

      public string HeaderHtml
      {
          get
          {
              if (!string.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"] != "0")
              {

                  tbCompany oCompany = BaseFactory.theCompany.GetModel(int.Parse(Request.QueryString["id"]));

                  string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                            + " 公司名称：{0} "
                                            + " <a style=\" display:block; float:right;\" href=\"javascript:location.href='companyMoneyList.aspx?id={1}&type={2}';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                            + " </div>"
                                            , oCompany.Name
                                            , oCompany.ParentId.ToString()
                                            , ParamentType);
                  return sHead;
              }
              return string.Empty;

          }


      }

        public void Page_Load(object sender, EventArgs e)
        {
            this.ValidateLoginUser();
            if (!IsPostBack)
            {
                this.BindProvenctList();
                this.BindCompanyLevel();
                this.BindGridView(string.Empty);
            }
        }

        void ValidateLoginUser()
        {
        
                if (!base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoneyList))
                {
                    base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                    base.Response.End();
                }

                if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoneyIn))
                {
                    _isInMoney = true;
                }
                if (base.GetValidateLoginRule(Gift.Model.ENUM.ERule.CompanyMoneyOut))
                {
                    _isOutMoney = true;
                }
        }

        void BindGridView(string sOrderBy)
        {
            StringBuilder sbWhere = new StringBuilder();
            sbWhere.AppendFormat(" (id={0} or parentId = {0})", base.Administrator.CompanyId);


            if (this.txtKeywords.Text.Trim() != "")
            {
                sbWhere.AppendFormat(" and (Name like '%{0}%' or CompanyCode = '{0}' or CompanyShortName like '%{0}%')", this.txtKeywords.Text.Trim());
            }
            if (this.ddlLevel.SelectedItem.Value != "")
            {
                sbWhere.AppendFormat(" and LevelId = {0} ", this.ddlLevel.SelectedItem.Value);
            }
            if (this.ddlProvence.SelectedItem.Value != "")
            {
                sbWhere.AppendFormat(" and Provence = '{0}", this.ddlProvence.SelectedItem.Value);
            }


            DataTable dtResult = BaseFactory.theCompany.GetMoneyList(sbWhere.ToString()).Tables[0];//.GetList("", "");

            dtResult.DefaultView.Sort = sOrderBy == string.Empty ? " CompanyName Asc " : sOrderBy;
            if (dtResult != null)
            {

                this.GridView1.DataSource = dtResult.DefaultView;
                this.GridView1.DataBind();

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + dtResult.Rows.Count.ToString() + "个记录";
                if (dtResult.Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                if (this.GridView1.PageCount > 0)
                {
                    for (int i = 1; i <= this.GridView1.PageCount; i++)
                    {
                        this.ddlCurrentPage.Items.Add(i.ToString());
                    }
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }

 

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Ascending"] == null)
            {
                ViewState["Ascending"] = " desc ";
                this.BindGridView( e.SortExpression + " asc ");
            }
            else
            {
                this.BindGridView( e.SortExpression + ViewState["Ascending"].ToString());
                if (ViewState["Ascending"].ToString() == " asc ")
                {
                    ViewState["Ascending"] = " desc ";
                }
                else
                {
                    ViewState["Ascending"] = " asc ";
                }
            }

        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            string sIds = "";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
                }
            }
            if (sIds.Length > 0)
            {
                sIds = sIds.Remove(sIds.Length - 1);

                BaseFactory.theCompany.DeleteMany(sIds);
                base.Response.Write("<script>alert('批量删除成功!')</script>");
                this.BindGridView(string.Empty);
            }

        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.BindGridView(string.Empty);
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.BindGridView(string.Empty);
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        //当鼠标停留时更改背景色
                        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D7EDFD'");
                        //当鼠标移开时还原背景色
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

                        if (GridView1.DataKeys[e.Row.RowIndex].Value.ToString() == base.Administrator.CompanyId)
                        {
                            e.Row.FindControl("divInMoney").Visible = false;
                            e.Row.FindControl("divOutMoney").Visible = false;
                        }
                    }
            }
        }


        

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }


        void BindCompanyLevel()
        {
            DataSet ds = BaseFactory.theCompany.GetCompanyLevelList(base.Administrator.CompanyId);
            if (ParamentType == "publish")
            {
                ds.Tables[0].DefaultView.RowFilter = " Id <= 1 ";
            }
            else if (base.Administrator.CompanyId != null && base.Administrator.AdminTypeId != "1")
            {
                ds.Tables[0].DefaultView.RowFilter = " Id >= " + base.Administrator.CompanyLevelId;
            }
            this.ddlLevel.DataTextField = "LevelName";
            this.ddlLevel.DataValueField = "Id";
            this.ddlLevel.DataSource = ds.Tables[0].DefaultView;
            this.ddlLevel.DataBind();
            this.ddlLevel.Items.Insert(0, new ListItem("请选择商家级别...", ""));
        }

        protected void ibtnSearch_Click(object sender, ImageClickEventArgs e)
        {
            this.BindGridView(string.Empty);
        }

        void BindProvenctList()
        {
            DataSet ds = BaseFactory.theCompany.GetProvenceList(base.Administrator.CompanyId);
            this.ddlProvence.DataTextField = "provence";
            this.ddlProvence.DataValueField = "provence";
            this.ddlProvence.DataSource = ds;
            this.ddlProvence.DataBind();
            this.ddlProvence.Items.Insert(0, new ListItem("请选择省份", ""));
        }
    }
}